package com.sikadwa.analysis.http

import com.sikadwa.analysis.AnalysisBuilder
import com.sikadwa.syc.http.HttpRequestBuilder


object UrlHelper {

    fun combineUrl(): String {
        if (AnalysisBuilder.host.isBlank() or
            AnalysisBuilder.zone.isBlank() or
            AnalysisBuilder.pathCommon.isBlank()
        ) {
            return ""
        }

        val zone = AnalysisBuilder.zone
        AnalysisBuilder.zone = zone.replace("/", "")

        val pathCommon = AnalysisBuilder.pathCommon
        AnalysisBuilder.pathCommon = if (pathCommon.startsWith("/")) {
            pathCommon.replace("/", "")
        } else {
            pathCommon
        }

        return AnalysisBuilder.host + "/" + AnalysisBuilder.zone + "/" + AnalysisBuilder.pathCommon
    }
}

object HttpHelper {
    fun postData(toUrl: String, body: String) {
        if (toUrl.isBlank()) return
        if (body.isBlank()) return

        HttpRequestBuilder()
            .url(toUrl)
            .method("POST")
            .addHeader("product", "SIKADWA")
            .addHeader("appid", "SIKADWA")
            .body(body)
            .timeouts(20000, 20000)
            .execute(object : HttpRequestBuilder.Callback {
                override fun onSuccess(response: String?) {
                }
                override fun onFailure(code: Int, msg: String?) {
                }
                override fun onFinished() {
                }
            });
    }
}


